import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;

import java.awt.event.*;

import ignorethis.*;
import yourwork.*;


/**
 * Creates an applet that be viewed over the web.
 *
 * @author Robert C. Duvall
 */
@SuppressWarnings("serial")
public class Applet extends JApplet
{
    // constants
    public static final String MOVERS_LABEL = "Number of shapes to create";
    public static final int MIN_MOVERS = 1;
    public static final int MAX_MOVERS = 512;
    public static final String TRAIL_LABEL = "Length of trail to display";
    public static final int MIN_TRAIL = 0;
    public static final int MAX_TRAIL = 128;
    // animate 25 times per second if possible
    public static final int DEFAULT_DELAY = 1000 / 25;  // in milliseconds

    // state
    private Canvas myDisplay;
    private Timer myTimer;


    /**
     * Initializes the applet --- called by the browser.
     */
    public void init ()
    {
        // create container to display animations
        init(new Dimension(Integer.parseInt(getParameter("width")),
                           Integer.parseInt(getParameter("height"))));
    }


    /**
     * Initializes the applet --- called by main.
     */
    public void init (Dimension size)
    {
        // create container to display animations
        myDisplay = new Canvas(size);
        myDisplay.setPreferredSize(size);
        myDisplay.setSize(size);
        myDisplay.setFocusable(true);
        myDisplay.requestFocus();

        // create user interface controls
        RangeSlider movers = new RangeSlider(MOVERS_LABEL, MIN_MOVERS, MAX_MOVERS);
        RangeSlider trail = new RangeSlider(TRAIL_LABEL, MIN_TRAIL, MAX_TRAIL);
        JPanel sliders = new JPanel();
        
        sliders.add(movers, BorderLayout.NORTH);
        sliders.add(trail, BorderLayout.CENTER);
        // add commands to test here
        ButtonPanel commands = new ButtonPanel(myDisplay, movers, trail);
        commands.add(new BouncerFactory());
        commands.add(new RacerFactory());
        //commands.add(new WalkerFactory());
        //commands.add(new AttractorFactory());
        //commands.add(new SwarmFactory());

        // adds the button for "clear"
        JButton clearButton = new JButton("Clear");
        clearButton.addActionListener(new ActionListener()
            {
                public void actionPerformed (ActionEvent e)
                {
                    myDisplay.clear();
                }
            });
        commands.add(clearButton);
        
        // add our user interface components to applet
        getContentPane().add(commands, BorderLayout.NORTH);
        getContentPane().add(myDisplay, BorderLayout.CENTER);
        getContentPane().add(sliders, BorderLayout.EAST);
    }
    
    
    /**
     * Starts the applet's action, i.e., starts the animation.
     */
    public void start ()
    {
        // create a timer to animate the canvas
        myTimer = new Timer(DEFAULT_DELAY, 
            new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    myDisplay.update();
                    myDisplay.repaint();
                }
            });
        // start the animation
        myTimer.start();
    }


    /**
     * Stops the applet's action, i.e., the animation.
     */
    public void stop ()
    {
        // stop the animation
        myTimer.start();
    }
}
